require 'osx/cocoa'
include OSX
prefix = "com.apple.speech.synthesis.voice."
require 'enumerator'

class CocoaSpeak

  VOICES = NSSpeechSynthesizer.availableVoices

  def initialize
    @voice = VOICES[2]
    @volume = 1.0
    @speech = NSSpeechSynthesizer.alloc.initWithVoice(@voice)
    @speech.setVolume(@volume)
  end

  def set_voice(index)
    @voice = VOICES[index]
    @speech.setVoice(@voice)
  end

  def display_voices(current_index = nil)
    if current_index 
      current_index = current_index.to_i
    end
    VOICES.to_enum(:each_with_index).map {|x, i| "<option value='#{i}' #{current_index == i ? 'selected=selected' : nil }>" +  x.split('.')[-1] + "</option>" }.join("\n")
#    VOICES.map {|x| x.split('.')[-1] }
  end

  def speak(text)
    @speech.startSpeakingString(text)
  end
end
